﻿/************************************************************************

 This file is part of "10 Second Tim".

 "10 Second Tim" is free software: you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation, either version 3 of the License, or
 (at your option) any later version.

 "10 Second Tim" is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with "10 Second Tim".  If not, see <http://www.gnu.org/licenses/>.

************************************************************************/

// All code (c)2013 Scott Bevin. all rights reserved

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using LDE;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace LudumDare27.Games.Game_5
{
	public class RocketTerrain
	{
		public class Tile
		{
			public Vector2 Pos = Vector2.Zero;
			public float HeightDiv2;
		}

		private List<Tile> m_tiles = new List<Tile>( );

		private Texture2D m_sprite;
		private Vector2 m_origin;

		private float TileWidth;

		public RocketTerrain( GameScreen screen, int numtiles, float TileWidth )
		{
			this.TileWidth = TileWidth;

			Random rnd = new Random( );

			Vector2 pos = new Vector2( 0.0f, 0.0f );
			for ( int i = 0; i < numtiles; ++i )
			{
				Tile tile = new Tile( );

				if ( i < 5 )
					pos.Y += (float)rnd.NextDouble( ) * Defs.Game5.ChannelStep;
				else
					pos.Y += ( ( (float)rnd.NextDouble( ) * 2.0f ) - 1.0f ) * Defs.Game5.ChannelStep;

				pos.X += TileWidth;

				tile.Pos = pos;
				tile.HeightDiv2 = Defs.Game5.ChannelHeightMin + ( (float)rnd.NextDouble( ) * Defs.Game5.ChannelHeightExtra );
				tile.HeightDiv2 *= 0.5f;

				m_tiles.Add( tile );
			}

			m_sprite = screen.ContentManager.Load<Texture2D>( "Img/G4/BorderTile" );
			m_origin = new Vector2( m_sprite.Width * 0.5f, m_sprite.Height * 0.5f );
		}

		public void Render( SpriteBatch sprites, Character c )
		{
			for ( int i = 0; i < m_tiles.Count; ++i )
			{
				Tile tile = m_tiles[ i ];

				//tile = GetTileAtPos( c.Position.X );

				Vector2 top = tile.Pos; top.Y -= ( tile.HeightDiv2 );
				Vector2 bot = tile.Pos; bot.Y += ( tile.HeightDiv2 );

				sprites.Draw( m_sprite, top, null, Color.White, 0.0f, m_origin, 1.0f, SpriteEffects.FlipVertically, 1.0f );
				sprites.Draw( m_sprite, bot, null, Color.White, 0.0f, m_origin, 1.0f, SpriteEffects.None, 1.0f );
			}
		}

		public Tile GetTileAtPos( float x )
		{
			float xPos = x;
			int tile = (int)( ( xPos /*+ ( TileWidth * 0.5f )*/ ) / TileWidth );

			if ( tile < 0 )
				tile = 0;

			if ( tile >= m_tiles.Count )
				tile = m_tiles.Count - 1;

			return m_tiles[ tile ];
		}
	}
}